package com.crud.project.controller;

import com.crud.project.dao.IResourceDao;
import com.crud.project.po.Course;
import com.crud.project.po.Resource;
import com.crud.project.service.CourseService;
import com.crud.project.service.ResourceService;
import com.crud.project.util.MD5Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class ResourceController {
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private CourseService courseService;

    @GetMapping("/resource")
    public String findAllUser(@RequestParam(required=true,defaultValue="1") Integer page, Model model){
        PageHelper.startPage(page,3);
        List<Resource> resourceList=resourceService.findAllResource();
        PageInfo<Resource> pageInfo=new PageInfo<>(resourceList);
        model.addAttribute("resource",pageInfo);
        return  "resource";
    }

    @GetMapping("/resource-input")
    public String second(Model model, HttpSession httpSession){
        if (httpSession.getAttribute("resource")==null){
            List<Course>courseList=courseService.findAllCourse();
            Resource resource=new Resource();
            resource.setResource_course(courseList);
            model.addAttribute("resource",resource);
        }
        return "resource-input";
    }

    @PostMapping("/insertReource")
    public String insertreReource(Resource resource, RedirectAttributes redirectAttributes){
        resource.setResource_creatTime(new Date());
        List<Resource> resourceList=resourceService.findAllResource();
        for (Resource resource1:resourceList){
            if (resource1.getResource_name()==resource.getResource_name()&&resource1.getResource_uploader()==resource.getResource_uploader()){
                redirectAttributes.addFlashAttribute("message","请不要提交重复数据");
                return "redirect:/resource";
            }
        }
        resourceService.insertResource(resource);
        Long resource_id=Long.parseLong(resourceService.findResourceIdByResource(resource));
        String course_id=resource.getCourseString();
        List<Long> list = new ArrayList<>();
        if (!"".equals(course_id) && course_id != null) {
            String[] idarray = course_id.split(",");
            for (int i=0; i < idarray.length;i++) {
                list.add(new Long(idarray[i]));
            }
        }
        for (Long id : list){
            resourceService.insertCourse_resourse(id,resource_id);
        }
        return "redirect:/resource";
    }

    @GetMapping("/updateSource-input/{id}")
    public String updateInput(@PathVariable Long id, Model model){
        Resource resource=resourceService.findResourceById(id);
        List<String> list=resourceService.findResource_courseId(Long.parseLong(resource.getResource_id()));
        String courseString= StringUtils.join(list,",");
        resource.setCourseString(courseString);
        resource.setResource_creatTime(new Date());
        resource.setResource_course(courseService.findAllCourse());
        model.addAttribute("resource",resource);
        return "resource-input";
    }

    @PostMapping("/updateResource/{id}")
    public String updateRole(@PathVariable Long id,Resource resource,Model model){
        Resource resource1=resourceService.findResourceById(id);
        resource.setResource_id(resource1.getResource_id());
        BeanUtils.copyProperties(resource,resource1);
        resourceService.deleteResource_course(Long.parseLong(resource1.getResource_id()));
        String[]ids=resource1.getCourseString().split(",");
        for (int i=0;i<ids.length;i++){
            resourceService.insertCourse_resourse(Long.parseLong(ids[i]),Long.parseLong(resource1.getResource_id()));
        }
        resource1.setResource_creatTime(new Date());
        resourceService.updateResource(resource1);
        return "redirect:/resource";
    }

    @GetMapping("/deleteSource/{id}")
    public String deleteById(@PathVariable Long id){
        resourceService.deleteResourceById(id);
        resourceService.deleteResource_course(id);
        return "redirect:/resource";
    }
}
